﻿using OvenParameterForm.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OvenParameterForm.Technics
{
    public class 加热 : TechnicsBaseClass
    {
        public string 加热设定温度 { get; set; }
        public string 超温报警值 { get; set; }
        public string 超温停机值 { get; set; }
        public string 降温报警值 { get; set; }
        private double 当前温度1;
        private double 当前温度2;
        private double 当前温度3;
        private double 当前温度4;
        private double 设定目标温度;
        public 加热(string[] strings) : base(strings)
        {
            加热设定温度 = strings[1];
            超温报警值 = strings[2];
            超温停机值 = strings[3];
            降温报警值 = strings[4];
        }
        public 加热(string name) : base(name)
        {
        }

        public override void Run()
        {
            设定目标温度 = Convert.ToDouble(加热设定温度);
            PLC.WriteData(ovenPLCaddress.加热目标地址, (int.Parse(加热设定温度) * 10).ToString());
            PLC.WriteData(ovenPLCaddress.超温报警地址,(int.Parse( 超温报警值)*10).ToString());
            PLC.WriteData(ovenPLCaddress.超温停机地址, (int.Parse(超温停机值) * 10).ToString() );
            PLC.WriteData(ovenPLCaddress.降温报警地址, (int.Parse(降温报警值) * 10).ToString() );
            PLC.WriteData(ovenPLCaddress.写入参数地址,"1");
            PLC.WriteData(ovenPLCaddress.加热地址, "1");
            PLC.WriteData(ovenPLCaddress.烘箱保温地址, "0");
            base.Run();
        }
        protected override void updatetime(object state)
        {
            当前温度1 = Convert.ToDouble(PLC.ReadData(ovenPLCaddress.层1温度地址))/10;
            当前温度2 = Convert.ToDouble(PLC.ReadData(ovenPLCaddress.层2温度地址))/10;
            当前温度3 = Convert.ToDouble(PLC.ReadData(ovenPLCaddress.层3温度地址))/10;
            当前温度4 = Convert.ToDouble(PLC.ReadData(ovenPLCaddress.层4温度地址))/10;
            sendStatus($"正在加热,当前温度：层1温度：{当前温度1},层2温度：{当前温度2},层3温度：{当前温度3},层4温度：{当前温度4}");
        }

        protected override bool Complete()
        {
           // 设定目标温度 = Convert.ToDouble(加热设定温度);
            if (当前温度1 - 设定目标温度 >= -2 && 当前温度2 - 设定目标温度 >= -2 && 当前温度3 - 设定目标温度 >= -2 && 当前温度4 - 设定目标温度 >= -2)
            {
                PLC.WriteData(ovenPLCaddress.加热地址, "0");
                return true;
            }
            else
            {
                return false;
            }
        }

        protected override void ClearVariable()
        {
            PLC.WriteData(ovenPLCaddress.加热地址, "0");
        }
    }
}
